using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using System;
using QFramework;

public class CoinUpgradeItem
{
    public string Key { get; private set; } //key值
    public string Description { get; private set; } //描述，用于设置button里text的显示
    
    public int Price { get; private set; } //价格，升级时消耗的金币数量

    public bool UpgradeFinish { get; set; } = false; // 是否已经升级完成

    public EasyEvent OnChange = new EasyEvent();

    private CoinUpgradeItem mNext = null; // 下一级数据

    public CoinUpgradeItem Next(CoinUpgradeItem next)
    {
        mNext = next;
        mNext.Condition(_ => UpgradeFinish);
        return mNext;
    }

    public void Upgrade()
    {
        mOnUpgrade?.Invoke(this);
        UpgradeFinish = true;
        TriggerOnChange();
        CoinUpgradeSystem.OnCoinUpgradeSystemChanged.Trigger();
    }

    public void TriggerOnChange()
    {
        OnChange.Trigger();
        mNext?.TriggerOnChange();
    }

    public bool ConditionCheck()
    {
        if (mCondition!=null)
        {
            return !UpgradeFinish&&mCondition.Invoke(this);
        }

        return !UpgradeFinish;
    }

    private Action<CoinUpgradeItem> mOnUpgrade;//用<>的目的是在调用方法的时候可以把当前CoinUpgradeItem返回出去，可以在外面调用

    private Func<CoinUpgradeItem, bool> mCondition;//当前的状态，是否已经升级过
    
    public CoinUpgradeItem Withkey(string key)
    {
        Key = key;
        return this;
    }

    public CoinUpgradeItem WithDescription(string description)
    {
        Description = description;
        return this;
    }

    public CoinUpgradeItem WithPrice(int price)
    {
        Price = price;
        return this;
    }

    public CoinUpgradeItem OnUpgrade(Action<CoinUpgradeItem> onUpgrade)
    {
        mOnUpgrade = onUpgrade;
        return this;
    }

    public CoinUpgradeItem Condition(Func<CoinUpgradeItem, bool> condition)
    {
        mCondition = condition;
        return this;
    }
}
